<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelFee extends JModel
{
	/**
	 * Fee data
	 *
	 * @var object
	 */
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	*/
	function __construct(){
		parent::__construct();

		$id = JRequest::getVar('id');
		$this->setId((int)$id);
	}

	/**
	 * Method to set the fee id
	 *
	 * @access	public
	 * @param	int	fee ID number
	 */
	function setId($id) {
		// Set new fee ID and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Overridden get method to get properties fee the data
	 *
	 * @access	public
	 * @param	string	$property	The name of the property
	 * @param	mixed	$value		The value of the property to set
	 * @return 	mixed 				The value of the property
	 * @since	1.0
	 */
	function get($property, $default=null) {
		/*
		* Load the fee data
		*/
		if ($this->_loadData()) {
			if(isset($this->_data->$property)) {
				return $this->_data->$property;
			}
		}
		return $default;
	}

	/**
	 * Method to get fee data
	 *
	 * @access public
	 * @return object
	*/
	function &getData(){
		
		/*
		* Load the fee data
		*/
		if($this->_loadData()) {
			// Someting code
		} else {

			$row = new stdClass();
			$row->id			= null;
			$row->from			= null;
			$row->to			= null;
			$row->fee			= null;

			$this->_data = $row;
		}

		return $this->_data;
	}

	/**
	 * Method to load required data
	 *
	 * @access	private
	 * @return	array
	 * @since	1.0
	*/
	function _loadData() {

		//Lets load the fee if it doesn't already exist
		if(empty($this->_data)) {

			$query = ' SELECT * FROM #__ownservice_fees '
					.' WHERE id='.$this->_id;

			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();

			return (boolean) $this->_data;
		}

		return true;
	}

	/**
	 * Method to store the fee
	 *
	 * @access public
	 * @return boolean True on success
	*/
	function store($data) {

		//Check the request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$user = & JFactory::getUser();

		$row = & $this->getTable( 'ownservice_fee','' );

		if(!$row->bind($data)) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		// Make sure the data is valid
		if (!$row->check()) {
			$this->setError($row->getError() );
			return false;
		}

		// Store it in the db
		if (!$row->store()) {
			$this->setError( $this->_db->getErrorMsg() );
			return false;
		}

		$this->_data = $row;

		return true;
	}
	
	/**
	 * Method to get Exchange Rate
	 * 
	 * @access public
	 * @return object
	 * @since 1.5
	 *
	 */
	 function getExchangeRate($id)
	 {
	 	$query = 'SELECT c.* '
	 			.' FROM #__ownservice_currencies AS c '
	 			.' LEFT JOIN #__ownservice_exchangerates AS r ON r.currency_id=c.id'
				.' WHERE r.id='.$id;
				
		$this->_db->setQuery($query);
		$row = $this->_db->loadObject();
	 	return $row;
	 }
}